package com.icoolkj.mall.inventory.controller;

import com.icoolkj.mall.common.Result;
import com.icoolkj.mall.inventory.dto.InventoryRequest;
import com.icoolkj.mall.inventory.dto.InventoryResponse;
import com.icoolkj.mall.inventory.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/01/15 20:34
 */
//@CrossOrigin
@RestController
@RequestMapping("/api/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    @PostMapping("/reduce-inventory")
    public Result<?> reduceInventory(@RequestBody InventoryRequest inventoryRequest){
        try {
            inventoryService.reduceInventory(inventoryRequest.getProductId(), inventoryRequest.getInventoryQuantity());
        } catch (RuntimeException e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @GetMapping("/get-inventory-quantity")
    public Result<?> getInventoryQuantity(@RequestParam("productId") Long productId){
        return inventoryService.getInventoryQuantity(productId);
    }

}
